package com.ziyun.erp.modules.order.service.impl;

import com.ziyun.erp.common.e.DeleteEnum;
import com.ziyun.erp.modules.order.dao.OrderFilesDao;
import com.ziyun.erp.modules.order.dao.OrderRemarksDao;
import com.ziyun.erp.modules.order.entity.*;
import com.ziyun.erp.modules.order.vo.UserAccessoryVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import com.ziyun.erp.modules.order.dao.UserAccessoryDao;
import com.ziyun.erp.modules.order.service.UserAccessoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("userAccessoryService")
public class UserAccessoryServiceImpl extends ServiceImpl<UserAccessoryDao, UserAccessoryEntity> implements UserAccessoryService {

    @Autowired
    private OrderRemarksDao orderRemarksDao;
    @Autowired
    private OrderFilesDao orderFilesDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<UserAccessoryEntity> page = this.selectPage(
                new Query<UserAccessoryEntity>(params).getPage(),
                new EntityWrapper<UserAccessoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<UserAccessoryVo> queryListByItemId(String itemId) {
        if (StringUtils.isBlank(itemId)){
            return null;
        }
        return this.baseMapper.queryListByItemId(itemId);
    }

    @Override
    public List<UserAccessoryVo> queryListByOrderNo(String orderNo) {
        if (StringUtils.isBlank(orderNo)){
            return null;
        }
        return baseMapper.queryListByOrderNo(orderNo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveUserAccessor(UserAccessoryVo userAccessoryVo,Long userId) {
        // 订单附件
        UserAccessoryEntity userAccessory = new UserAccessoryEntity();
        userAccessory.setUrl(userAccessoryVo.getUrl());
        userAccessory.setName(userAccessoryVo.getName());
        userAccessory.setSize(userAccessoryVo.getSize());
        userAccessory.setUserId(userAccessoryVo.getUserId());
        userAccessory.setCreateId(userId.toString());
        userAccessory.setFileType(userAccessoryVo.getFileType());
        this.baseMapper.insert(userAccessory);

        // 订单附件关联
        OrderFilesEntity orderFiles = new OrderFilesEntity();
        orderFiles.setFileId(userAccessory.getId());
        orderFiles.setOrderId(Long.valueOf(userAccessoryVo.getOrderId()));
        orderFiles.setOrderItemId(userAccessoryVo.getItemId());
        orderFilesDao.insert(orderFiles);

        // 备注
        if (!StringUtils.isBlank(userAccessoryVo.getRemark())){
            OrderRemarksEntity orderRemarksEntity = new OrderRemarksEntity();
            orderRemarksEntity.setOrOrderId(Long.valueOf(userAccessoryVo.getOrderId()));
            orderRemarksEntity.setOrRemarks(userAccessoryVo.getRemark());
            orderRemarksEntity.setOrCreateName(userId.toString());
            orderRemarksEntity.setOrItemId(userAccessoryVo.getItemId());
            orderRemarksDao.insert(orderRemarksEntity);
        }

    }

    @Override
    public void deleteUserAccessor(Integer[] ids) {
        Map<String, Object> param = new HashMap<>();
        param.put("disabled", DeleteEnum.DELETE.toInt());
        this.baseMapper.updateByMap(param, ids);
    }

    @Override
    public void saveAllotOrderFiles(List<UserAccessoryVo> userAccessoryVos, String orderId, Integer itemId, Integer userId) {
        for (UserAccessoryVo userAccessoryVo : userAccessoryVos){
            UserAccessoryEntity userAccessory = new UserAccessoryEntity();
            userAccessory.setUserId(userId);
            userAccessory.setUrl(userAccessoryVo.getUrl());
            userAccessory.setName(userAccessoryVo.getName());
            userAccessory.setSize(userAccessoryVo.getSize());
            userAccessory.setFileType(userAccessoryVo.getFileType());
            this.baseMapper.insert(userAccessory);

            OrderFilesEntity orderFiles = new OrderFilesEntity();
            orderFiles.setFileId(userAccessory.getId());
            orderFiles.setOrderId(Long.valueOf(orderId));
            orderFiles.setOrderItemId(itemId);
            orderFilesDao.insert(orderFiles);
        }
    }

}
